USE [ArtsShop]
GO

/****** Object:  StoredProcedure [dbo].[getPopularItems]    Script Date: 02/23/2011 07:34:36 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[getPopularItems]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[getPopularItems]
GO

USE [ArtsShop]
GO

/****** Object:  StoredProcedure [dbo].[getPopularItems]    Script Date: 02/23/2011 07:34:36 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

-- =============================================
-- Author:		Francis A
-- Create date: 08/02/2011
-- Description:	Get the most popular items based on their order preference
-- =============================================
CREATE PROCEDURE [dbo].[getPopularItems]
	-- Add the parameters for the stored procedure here
	@numItems int = 5
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	select distinct top (@numItems) pd.modelName,pd.productId,(select SUM(quantity) from orderDetail where productId=pd.productId) as qty from orderDetail od
	inner join Product pd on
	od.productId = pd.productId
	order by qty desc
END

GO


